﻿
using Admin.NET.Application.Service;

namespace Admin.NET.Application.Service;
public partial class TraShenQingService
{



    public async Task p() { 
    
    }


    //MiddleAudit 上级医生审核
    //DepartmentAudit //科主任审核
    // Audit(TraApplyBillAuditDto input) 输血科审核

    //private readonly IdentityService _identityService;
    ///
    public async Task CheckTraZhuanTai(CheckTraZhuanTaiInput input) {

        //状态判断
        ShenHePanDuan zhuangTai = new zhuangTaiShenHePanDuan();
        //职称判断
        ShenHePanDuan zhiCheng = new zhuangTaiShenHePanDuan();
        //权限判断
        ShenHePanDuan quanXian = new quanXianShenHePanDuan();
        //相同科室
        ShenHePanDuan xiangTongKeShi = new xiangTongKeShiShenHePanDuan();
        //添加状态判断
        zhuangTai.SetSuccessor(quanXian);
        //添加相同科室判断
        quanXian.SetSuccessor(xiangTongKeShi);


        var tra=Queryable.InSingle(input.Id);
        var userId = input.shenHeRenId ?? _userManager.UserId;
        var user = await _sysUserService.GetUserInfoById(userId);
        // 用户科室id，附属机构 + 用户org
        var userKeShiIds = user.ExtOrgIds;
        user.ExtOrgIds.Add(user.OrgId);

        var panDuanInput = new CheckShenHeXinXiInput
        { 
            danJuZhuangTai = tra.danJuZhuangTai,
            keShiId = tra.kaiDanKeShiId,
            userKeShiIds = user.ExtOrgIds,
            CreateUserId = tra.CreateUserId.Value,
            permList = user.Buttons,
            zhiCheng = user.PosName
        };

        if (input.zhuangTai == DanJuZhuangTai.待上级审核) {

            panDuanInput.muBiaoDanJuLeiXing = DanJuZhuangTai.待输血科审核;
            panDuanInput.quanXian = "traShuXueShengQing:shangJiYiShengShenHe";//权限
            panDuanInput.muBiaoZhiCheng = ZhiCheng.副高;//目标职称
            //职称判断
            xiangTongKeShi.SetSuccessor(zhiCheng);
        }
        else if (input.zhuangTai == DanJuZhuangTai.待科主任审核)
        {
            panDuanInput.muBiaoDanJuLeiXing = DanJuZhuangTai.待输血科审核;
            panDuanInput.quanXian = "traShuXueShengQing:linChuangKeZhuRenShenHe";//权限
            panDuanInput.muBiaoZhiCheng = ZhiCheng.高级;//目标职称
            //职称判断
            xiangTongKeShi.SetSuccessor(zhiCheng);


        }
        else if (input.zhuangTai == DanJuZhuangTai.待输血科审核)
        {
            panDuanInput.muBiaoDanJuLeiXing = DanJuZhuangTai.已审核;
            panDuanInput.quanXian = "traShuXueShengQing:shuXueKeShenHe";//权限
            panDuanInput.keShiId = 1;//判断科室  写死输血科
        }

        zhuangTai.HandleRequest(panDuanInput);

    }



    /// <summary>
    /// 上级医生审核
    /// </summary>
    /// <returns></returns>
    public async Task ShangJiYiShengShenHe(ShenQingShenHeInput input) {

       await  CheckTraZhuanTai(new CheckTraZhuanTaiInput {Id =input.Id,zhuangTai= DanJuZhuangTai.待上级审核, shenHeRenId = input.shenHeRenId });

        await _rep.UpdateAsync(u => new TraShenQing
        {
            danJuZhuangTai = DanJuZhuangTai.待输血科审核,
            shangJiShenHeRen = input.shenHeRen,
            shangJiShenHeRenId = input.shenHeRenId,
            shangJiShenHeShiJian = DateTime.Now,
            shangJiShenHeXinXi = input.shenHeXinXi
        }, u => u.Id == input.Id);

    }


    /// <summary>
    /// 科主任审核
    /// </summary>
    /// <returns></returns>
    public async Task KeZhuRenShenHe(ShenQingShenHeInput input) {

        await CheckTraZhuanTai(new CheckTraZhuanTaiInput { Id = input.Id, zhuangTai = DanJuZhuangTai.待科主任审核, shenHeRenId = input.shenHeRenId });

        await _rep.UpdateAsync(u => new TraShenQing
        {
            danJuZhuangTai = DanJuZhuangTai.待输血科审核,
            keZhuRenShenHeRen = input.shenHeRen,
            keZhuRenShenHeRenId = input.shenHeRenId,
            keZhuRenShenHeShiJian = DateTime.Now,
            keZhuRenShenHeXinXi = input.shenHeXinXi
        }, u => u.Id == input.Id);

    }

    /// <summary>
    /// 输血科审核
    /// </summary>
    /// <returns></returns>
    public async Task ShuXueKeShenHe(ShenQingShenHeInput input)
    {
        await CheckTraZhuanTai(new CheckTraZhuanTaiInput { Id = input.Id, zhuangTai = DanJuZhuangTai.待输血科审核, shenHeRenId = input.shenHeRenId });


        await _rep.UpdateAsync(u => new TraShenQing
        {
            danJuZhuangTai = DanJuZhuangTai.已审核,
            shuXueKeShenHeRen = input.shenHeRen,
            shuXueKeShenHeRenId = input.shenHeRenId,
            shuXueKeShenHeShiJian = DateTime.Now,
            shuXueKeShenHeXinXi = input.shenHeXinXi
        }, u => u.Id == input.Id);
    }

}
